define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'device/devicelock/index' + location.search,
                    add_url: 'device/devicelock/add',
                    edit_url: 'device/devicelock/edit',
                    del_url: 'device/devicelock/del',
                    multi_url: 'device/devicelock/multi',
                    import_url: 'device/devicelock/import',
                    table: 'device_lock',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                singleSelect:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'lockAlias', title: __('名字'), operate: 'LIKE'},
                        {field: 'lockMac', title: __('Mac地址'), operate: false},
                        {field: 'lockId', title: __('锁ID'), operate: 'LIKE'},
                        {field: 'electricQuantity', title: __('电量'), operate: false},
                        {field: 'shop.name', title: __('店铺'), operate: 'LIKE'},
                        {
                            field: 'room.name',
                            title: __('包厢'),
                            operate: 'LIKE',
                            align: 'left',
                            formatter: function (value, row, index, field) {
                                if (value == null) {
                                    return "";
                                }
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + value + "'>" + value + "</span>";
                            },
                            cellStyle: function (value, row, index, field) {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "220px"
                                    }
                                };
                            }
                        },
                        {field: 'is_gateway', title: __('是否网关'), searchList: {"1": __('网关'), "-1": __('非网关')}},
                        {
                            field: 'operate',
                            width: "100px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                        },]
                ]
            });

            //同步蓝牙锁
            let currentPage = 1;
            $('.btn-lockSync').on('click',function () {

                fetchData(currentPage);
            });

            function fetchData(page) {
                layer.msg('同步智能锁。。。', {icon: 16, shade: 0.01, time: 30 * 1000});
                $.ajax({
                    url:'/admin/device/Devicelock/refreshLocks',
                    method:'POST',
                    data:{page:page},
                    success: function(res) {
                        if (res.data.pageNo === res.data.pages) {
                            layer.closeAll();
                            //刷新当前页面
                            window.location.reload();
                            return;
                        }

                        // 递归调用下一页
                        currentPage++;
                        fetchData(currentPage);
                    },
                    error: function(error) {
                        console.error('请求失败:', error);
                    }
                });
            }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {

            $("#c-room_id").data("params", function () {
                return {custom: {shop_id: $("#c-shop_id").val()}};
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
